致胜优势INFO
联系我们CONTACT

公司地址:茂名市人民南路新村大院22号101

电话:13592986386

SQL Server Error Log管理您当前的位置:首页 > SQL Server Error Log管理

SQL Server Error Log管理

发布时间:2014/7/19 17:11:17

SQL Server Error Log管理
 
SQL Server除了每个数据库有Log文件,即LDF文件外,SQL Server自己本身也有一个Error Log,位置存在安装目录下,Widows系统中log文件位于C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Log。
  www.2cto.com  
Error log的文件数目可以配置,最多99个,默认6个,文件编号为ERRORLOG.1,2,3,4.....,还有一个文件没有编号,就叫ERRORLOG,这个文件是SQL Server当前的活动日志文件(active log),这个文件无法删除,其他的都可以删除。也就是说,ERRORLOG文件里是最新的日志信息,其他的文件都是以前的信息。
 
每次SQL Server重启动,所有的ERRORLOG文件要做一次切换,拿6个log文件来说,具体如下:
 
删除ERRORLOG.6中的所有数据   www.2cto.com  
 
ERRORLOG.5的数据写入到ERRORLOG.6中
 
ERRORLOG.4的数据写入到ERRORLOG.5中
 
ERRORLOG.3的数据写入到ERRORLOG.4中
 
ERRORLOG.2的数据写入到ERRORLOG.3中
 
ERRORLOG.2的数据写入到ERRORLOG.1中
 
ERRORLOG的数据写入到ERRORLOG.1中
 
重新创建ERRORLOG文件
 
所以,如果SQL Server不重启,ERRORLOG文件就会变得很大,解决办法如下:
 
1. 如果需要保存就得Error log文件,则先把当前的所有ERRORLOG文件copy到其他存储介质
  www.2cto.com  
2. 运行命令:  EXEC sp_cycle_errorlog,这个命令强制SQL Server做一次Error Log文件的切换
 
3. 把最大的log 文件删除
 
如果需定期切换error log,一般可使用DBCC errorlog命令在一个sql server的agent job 来完成。
 
create procedure sp_cycle_errorlog  
if (not (is_srvrolemember('sysadmin') = 1))  -- Make sure that it is the sysadmin role to execute the code.
    begin
        return -- here can raise an error.
 return(1)
    end
 
dbcc errorlog
return (0)